package org.projectwithoutname.ui.util;

import java.io.File;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.widgets.Shell;
import org.projectwithoutname.application.CriarThumbnail;
import org.projectwithoutname.commons.Commons;
import org.projectwithoutname.ui.wizard.ImageHandlerWizard;

public class ImageUtil implements Commons{

	private ImageData imageData;
	private Shell shell;
	private int x, y;
	private String name;
	private String filename;
	
	public ImageUtil(String path, Shell shell, String name){
		imageData = new ImageData(path);
		imageData.depth = 24;
		initialize();
		this.shell = shell;
		this.name = name;
	}
	
	public Image generate(Device device){
		return new Image(device, imageData);
	}
	
	private void initialize() {
		x = imageData.width;
		y = imageData.height;
		if (imageData.height < HEIGHT){
			for (float i=1.1F; i<=3; i +=0.1F){
				if((int)(imageData.height * i) >= HEIGHT){
					y = (int) (imageData.height * i);
					x = (int) (imageData.width * i);
					break;
				}
			}
		}
		else{
			if (imageData.height > HEIGHT){
				for (float i=1.0F; i>0; i -=0.1F){
					if((int)(imageData.height * i) <= HEIGHT){
						y = (int) (imageData.height * i);
						x = (int) (imageData.width * i);
						break;
					}
				}
			}
		}
	}
	
	public void resize(int width, int heigth){
		if (isAxisEquals(width, heigth))
			imageData = save(imageData.scaledTo(width, heigth));
		else{
			try {
				ImageHandlerWizard wizard = new ImageHandlerWizard(imageData);
				WizardDialog dialog = new WizardDialog(shell, wizard);
				dialog.setPageSize(x, y);
				dialog.open();
				imageData = save(wizard.getCut());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	private ImageData save(ImageData data){
		File aux = new File(ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString()+"/Workspace");
		ImageLoader loader = new ImageLoader();
		loader.data = new ImageData[] {data};
		File file = new File(aux.getAbsolutePath()+"/images");
		if (!file.exists())
			file.mkdir();
		filename = file.getAbsolutePath()+"/"+name+".jpg";
		loader.save(filename, SWT.IMAGE_JPEG);
		try {
			CriarThumbnail.risize(filename, filename, SELECTION_WIDTH, SELECTION_HEIGHT, 100, true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ImageData(filename);
	}
	
	public ImageData getImageData(){
		return imageData;
	}
	
	public String getPath(){
		return filename;
	}
	
	private boolean isAxisEquals(int width, int heigth){
		long ratio = width/heigth;
		long aspectRatio = imageData.width / imageData.height;
		return aspectRatio == ratio? true : false;
	}
}
